package com.wtwd.campus.controller;

import com.wtwd.campus.common.RespCode;
import com.wtwd.campus.common.RespEntity;
import com.wtwd.campus.entity.FenceInfo;
import com.wtwd.campus.service.FenceInfoService;
import com.wtwd.campus.utils.GouldUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/fence")
public class FenceController {
    private final static Logger logger = LoggerFactory.getLogger(FenceController.class);
    @Autowired
    private FenceInfoService fenceInfoService;
    @Autowired
    private GouldUtil gouldUtil;

    /**
     * 添加围栏
     *
     * @param fenceInfo
     * @return
     */
    @PostMapping("/addFence")
    public RespEntity addFence(@RequestBody FenceInfo fenceInfo) {
        logger.info("addFence: fenceInfo = {}" , fenceInfo.toString());
        Integer i = fenceInfoService.addFence(fenceInfo);
        return new RespEntity(i > 0 ? RespCode.SUCCESS : RespCode.FAILED);
    }


    /**
     * 条件获取围栏列表
     *
     * @param orgCode
     * @param fenceName
     * @param currPage
     * @param pageSize
     * @return
     */
    @GetMapping("/getFenceList/{orgCode}/{fenceName}/{currPage}/{pageSize}")
    public RespEntity getFenceList(@PathVariable("orgCode") String orgCode, @PathVariable("fenceName") String fenceName,
                                   @PathVariable("currPage") Integer currPage, @PathVariable("pageSize") Integer pageSize) {
        logger.info("getFenceList: schoolId = {},fenceName = {},currPage = {},pageSize = {}" , orgCode ,fenceName , currPage , pageSize);
        if(StringUtils.isBlank(fenceName)){
            fenceName=null;
        }
        Map<String, Object> result = fenceInfoService.queryFenceList(orgCode, fenceName, currPage, pageSize);
        return new RespEntity(RespCode.SUCCESS, result);
    }

    /**
     * 修改围栏信息
     *
     * @param fenceInfo
     * @return
     */
    @PutMapping("/modifyFence")
    public RespEntity modifyFence(@RequestBody FenceInfo fenceInfo) {
        logger.info("modifyFence: fenceInfo = {}" + fenceInfo);
        Integer i = fenceInfoService.modifyFenceInfo(fenceInfo);
        return new RespEntity(i > 0 ? RespCode.SUCCESS : RespCode.FAILED);
    }

    /**
     * 删除围栏
     *
     * @param fenceId
     * @return
     */
    @DeleteMapping("/deleteFence/{fenceId}")
    public RespEntity deleteFence(@PathVariable("fenceId") Integer fenceId) {
        logger.info("deleteFence: fenceId = {}" + fenceId);
        Integer i = fenceInfoService.deleteFenceInfo(fenceId);
        return new RespEntity(i > 0 ? RespCode.SUCCESS : RespCode.FAILED);
    }





    /* *//**
     * 根据位置查询经纬度
     *
     * @param place
     * @return
     *//*
    @GetMapping("/getLngAndLatByPlace/{place}")
    public RespEntity getLngAndLatByPlace(@PathVariable("place") String place) {
        logger.info("getLngAndLatByPlace: place = {}" + place);
        String lonLat = gouldUtil.getLonLat(place);
        String[] split = lonLat.split(",");
        Map<String, Double> map = new HashMap<>();
        map.put("longitude", Double.parseDouble(split[0]));
        map.put("latitude", Double.parseDouble(split[1]));
        return new RespEntity(RespCode.SUCCESS, map);
    }*/

}
